package phoenix.core.helpers;

import com.hp.gagawa.java.Document;
import com.hp.gagawa.java.DocumentType;
import com.hp.gagawa.java.elements.*;
import phoenix.core.PhoenixManager;
import phoenix.core.base.HelperBase;

import java.util.List;

public class HtmlHelper extends HelperBase
{
	public HtmlHelper(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public String addLinksCoverage()
	{
		Document document = new Document(DocumentType.XHTMLStrict);
		Head htmlHead = new Head();
		htmlHead.appendText(
				"<link href=\"" + phoenix.config().REDMINE_HOST + "/stylesheets/application.css\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" />");
		document.head.appendChild(htmlHead);

		Div div = new Div();
		div.setId("table_additional_info").setCSSClass("results");
		A linkWebCoverage = new A(
				"https://docs.google.com/spreadsheets/d/1rzNyR02Ho38d1M3yJJ_7703_bfpRkPGh8xQu6ea9o5A/edit#gid=510116601");
		linkWebCoverage.appendText("Web coverage");

		A linkMobCoverage = new A(
				"https://docs.google.com/spreadsheets/d/1rzNyR02Ho38d1M3yJJ_7703_bfpRkPGh8xQu6ea9o5A/edit#gid=1245537793");
		linkMobCoverage.appendText("Mob coverage");

		div.appendChild(new Br());
		div.appendChild(linkWebCoverage);
		div.appendChild(new Br());
		div.appendChild(linkMobCoverage);
		div.appendChild(new Br());
		document.body.appendChild(div);
		return document.write();
	}

	/**
	 * Used for generating HTML message of redmine report
	 * 
	 * @param p_listOfIssues
	 * @return
	 */
	public String buildReportTable(List<String[]> p_listOfIssues)
	{
		Document document = new Document(DocumentType.XHTMLStrict);
		Head htmlHead = new Head();
		htmlHead.appendText(
				"<link href=\"" + phoenix.config().REDMINE_HOST + "/stylesheets/application.css\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" />");

		document.head.appendChild(htmlHead);
		Div div = new Div();
		div.setId("table_results").setCSSClass("results");
		Table myTable = new Table();
		myTable.setBorder("1");
		myTable.setTitle("Results");
		myTable.setWidth("100%");

		Tr trHead = new Tr();
		trHead.appendChild(new Td().appendChild(new Text("Issue ID")));
		trHead.appendChild(new Td().appendChild(new Text("Subject")));
		trHead.appendChild(new Td().appendChild(new Text("Project")));
		trHead.appendChild(new Td().appendChild(new Text("Status")));

		myTable.appendChild(trHead);

		for (int row = 0; row < p_listOfIssues.size(); row++)
		{
			Tr tr = new Tr();
			myTable.appendChild(tr);

			for (int col = 0; col < 4; col++)
			{
				Td td = new Td();
				tr.appendChild(td);
				if (col == 0)
				{
					A link = new A(phoenix.config().REDMINE_HOST + "/issues/" + p_listOfIssues.get(row)[col], "_blank");
					link.appendText(p_listOfIssues.get(row)[col]);
					td.appendChild(link);
					continue;
				}

				td.appendChild(new Text(p_listOfIssues.get(row)[col]));
			}
		}

		div.appendChild(myTable);
		document.body.appendChild(div);
		document.body.appendText(addLinksCoverage());
		return document.write();
	}
}
